package www.dashu.com.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import www.dashu.com.api.CommonResult;
import www.dashu.com.domain.CProductAttribute;
import www.dashu.com.domain.CProductAttributeValue;
import www.dashu.com.service.CProductAttributeService;
import www.dashu.com.service.CProductAttributeValueService;
import www.dashu.com.service.RedisService;
import www.dashu.com.domain.CProduct;
import www.dashu.com.dto.CProductQuery;
import www.dashu.com.service.CProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Root;
import java.util.*;

@Controller
@Api(tags = "ProductController", description = "商品信息")
@RequestMapping("/product")
public class ProductController
{
    @Autowired
    CProductService productService;
    @Autowired
    RedisService redisService;
    @Autowired
    CProductAttributeValueService attributeValueService;
    @Autowired
    CProductAttributeService attributeService;

    public static String[] SHOW_COLUMN = new String[]{"id","name","price","pic","serviceIds","brandName","companyName"};

    @ApiOperation(value = "商品信息列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Page<CProduct>> list(CProductQuery query) {
        try {
            Page<CProduct> pageData = productService.findAllIncludeColumn(Arrays.asList(SHOW_COLUMN) ,query);
            return CommonResult.success(pageData);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @ApiOperation(value = "商品明细")
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CProduct> detail(@RequestParam Long productId) {

        CProduct product = productService.findById(productId).get();

        List<CProductAttribute> attributeList = new ArrayList<>();

        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("productId", product.getId());
        List<CProductAttributeValue> attributeValueList = attributeValueService.findAll(queryMap);

        if(null != attributeValueList){
            attributeValueList.forEach(attribute ->{
                CProductAttribute attr = attributeService.findById(attribute.getProductAttributeId()).get();
                attr.setProductId(product.getId());
                attr.setValues(Arrays.asList(attribute.getValue().split(",")));
                if(!StringUtils.isEmpty(attribute.getOprations())){
                    attr.setOptions(JSON.parseArray(attribute.getOprations()));
                }else {
                    attr.setOptions(new ArrayList<>());
                }
                CProductAttribute attrTarget = new CProductAttribute();
                BeanUtils.copyProperties(attr, attrTarget);
                attributeList.add(attrTarget);
            });
        }

        product.setSelectProductAttr(attributeList);

        return CommonResult.success(product);

    }

    @ApiOperation(value = "新增商品信息")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody CProduct product) {
        product.setId(productService.createId(product));

        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());

        productService.save(product);
        return CommonResult.success();
    }

    @ApiOperation(value = "删除商品信息")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public CommonResult delete(@RequestBody CProduct product) {
        productService.deleteById(product.getId());
        return CommonResult.success();
    }

}
